package com.yjt.class_studio.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.class_studio.common.constant.Constant;
import com.yjt.class_studio.entity.*;
import com.yjt.class_studio.mapper.SysMenuMapper;
import com.yjt.class_studio.mapper.SysRoleMapper;
import com.yjt.class_studio.pojo.dto.SysUserLoginDTO;
import com.yjt.class_studio.pojo.dto.SysUserSearchDTO;
import com.yjt.class_studio.service.CmsStudioService;
import com.yjt.class_studio.service.CmsTeamService;
import com.yjt.class_studio.service.SysRoleService;
import com.yjt.class_studio.service.SysUserService;
import com.yjt.class_studio.mapper.SysUserMapper;
import com.yjt.class_studio.utils.RedisUtil;
import com.yjt.class_studio.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private CmsTeamService cmsTeamService;

    @Autowired
    private CmsStudioService cmsStudioService;

    @Autowired
    private RedisUtil redisUtil;



    @Override
    public SysUser getByUserName(String username) {
       return getOne(new QueryWrapper<SysUser>().eq("username",username));
    }

    @Override
    public String getUserAuthorityInfo(Long userId) {
        StringBuffer authority=new StringBuffer();

        if(redisUtil.hasKey(Constant.AUTHORITY_KEY+userId)){
            System.out.println("有缓存");
            authority.append(redisUtil.get(Constant.AUTHORITY_KEY,String.valueOf(userId)));
        }else{
            System.out.println("没缓存");
            // 获取角色
            List<SysRole> roleList = sysRoleMapper.selectList(new QueryWrapper<SysRole>().inSql("id", "select role_id from sys_user_role where user_id=" + userId));

            if(roleList.size()>0){
                String roleCodeStrs=roleList.stream().map(r->"ROLE_"+r.getCode()).collect(Collectors.joining(","));
                authority.append(roleCodeStrs);
            }

            // 获取菜单权限
            Set<String> menuCodeSet = new HashSet<String>();
            for(SysRole sysRole:roleList){
                List<SysMenu> sysMenuList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().inSql("id", "select menu_id from sys_role_menu where role_id=" + sysRole.getId()));
                for(SysMenu sysMenu:sysMenuList){
                    String perms=sysMenu.getPerms();
                    if(StringUtil.isNotEmpty(perms)){
                        menuCodeSet.add(perms);
                    }
                }
            }
            if(menuCodeSet.size()>0){
                authority.append(",");
                String menuCodeStrs = menuCodeSet.stream().collect(Collectors.joining(","));
                authority.append(menuCodeStrs);
            }
            redisUtil.set(Constant.AUTHORITY_KEY,String.valueOf(userId),authority,10*60);
            System.out.println("authority:"+authority.toString());
        }
        return authority.toString();
    }

    @Override
    public IPage getAllUserPageData(SysUserSearchDTO sysUserSearchDTO) {
        Page<SysUser> PageResult = this.page(new Page<>(sysUserSearchDTO.getPageNum(), sysUserSearchDTO.getPageSize()), new QueryWrapper<SysUser>().like(StringUtil.isNotEmpty(sysUserSearchDTO.getUsername()), "username", sysUserSearchDTO.getUsername()));
        List<SysUser> userList = PageResult.getRecords();
        System.out.println("查出前=================>"+userList.toString());

        for(SysUser user:userList){
            // 获取角色
            List<SysRole> roleList = sysRoleService.list(new QueryWrapper<SysRole>().inSql("id", "select role_id from sys_user_role where user_id=" + user.getId()));
            // 设置角色
            // user.setRoles(roleList.stream().map(SysRole::getName).collect(Collectors.joining(",")));
            user.setSysRoleList(roleList);

            // 查询工作室，团队
            if(user.getType() == 1){
                // 学生
                CmsTeam team = cmsTeamService.getById(user.getTeamId());
                user.setTeamName(team.getTeamName());
            }
            CmsStudio studio = cmsStudioService.getById(user.getStudioId());
            user.setStudioName(studio.getStudioName());
        }
        System.out.println("查出H后=================>"+userList.toString());
        return PageResult;
    }
}




